package com.itheima.social.securitys.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.social.securitys.mapper.UserSocialMapper;
import com.itheima.social.securitys.pojo.UserSocialSecurity;
import com.itheima.social.securitys.pojo.vo.User;
import com.itheima.social.securitys.pojo.vo.UserPersonalSocial;
import com.itheima.social.securitys.service.UserSocialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/6 17:59
 */
@Service
@Transactional
public class UserSocialServiceImpl implements UserSocialService {

    @Autowired
    private UserSocialMapper userSocialMapper;

    /**
     * 查询企业员工的社保信息列表
     */
    @Override
    public PageResult list(Integer page, Integer pageSize, String companyId) {
        PageHelper.startPage(page,pageSize);
        List<UserPersonalSocial> userCompanyPersonalList=userSocialMapper.findByPage(companyId);
        PageInfo<UserPersonalSocial> pageInfo=new PageInfo<UserPersonalSocial>(userCompanyPersonalList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 根据用户id查询用户的社保信息
     */
    @Override
    public UserSocialSecurity findUserSocialByUserId(String userId) {
        QueryWrapper<UserSocialSecurity> queryWrapper=new QueryWrapper<UserSocialSecurity>();
        queryWrapper.eq("user_id",userId);
        return userSocialMapper.selectOne(queryWrapper);
    }

    /**
     * 根据用户id查询用户信息
     */
    @Override
    public User findUserPersonalByUserId(String userId) {
        User user=userSocialMapper.findUserPersonalByUserId(userId);
        //根据companyId,部门id查询，部门名称
        String departmentName=userSocialMapper.findDepartmentNameByCompanyIdAndDepartmentId(user.getCompanyId(),user.getDepartmentId(),user.getDepartmentName());
        user.setDepartmentCode(departmentName);
        return user;
    }

    /**
     * 更新社保数据
     */
    @Override
    public Integer saveUserSocialSecurity(UserSocialSecurity userSocialSecurity) {
        String userId = userSocialSecurity.getUserId();
        QueryWrapper<UserSocialSecurity> queryWrapper=new QueryWrapper<UserSocialSecurity>();
        queryWrapper.eq("user_id",userId);
        //根据userId修改
        Integer rows = userSocialMapper.update(userSocialSecurity,queryWrapper);
        return rows;
    }
}
